<template>
  <div class="home-container">
    <van-nav-bar>
      <van-button
        slot="title"
        class="search"
        icon="search"
        type="info"
        round
        size="small"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <van-tabs v-model="active">
      <van-tab :title="channel.name" v-for="channel in channelLists" :key="channel.id" >
        <article-lists :channel="channel"/>
      </van-tab>
      <div
        slot="nav-right"
        class="wap-nav-placeholder"
      ></div>
      <div
        slot="nav-right"
        class="hamburger-tab"
        @click="onshowPopup = true"
      >
        <van-icon name="wap-nav" />
      </div>
      <van-popup
        v-model="onshowPopup"
        position="bottom"
        :style="{ height: '100%' }"
        close-icon-position="top-left"
        closeable
      >
        <channel-edit
          :channelLists="channelLists"
          :active="active"
          @update-active="onUpdateChannel"
          @close-channel="onCloseChannel"
        ></channel-edit>
      </van-popup>
    </van-tabs>
  </div>
</template>
<script>

// 导入子组件articlelists组件
import articleLists from './components/articleLists'

// 导入home组件需要使用的api接口
import { channels } from '@/api/home'

// 导入编辑频道列表组件
import channelEdit from '@/components/channelEdit'

import { mapState } from 'vuex'

import { getItem } from '@/utils/storage'
export default {
  name: 'homeIndex',
  data () {
    return {
      // 频道当前项
      active: 0,
      // 频道列表
      channelLists: [],
      // 是否展示弹出层
      onshowPopup: false
    }
  },
  components: {
    // 文章列表公用组件
    articleLists,
    // 频道编辑公用组件
    channelEdit
  },
  methods: {
    // 获取频道列表
    async getchannels () {
      let newChannels = []
      if (this.user) {
        const res = await channels()
        newChannels = res.data.data.channels
      } else {
        // 没有登录，判断是否有本地存储的频道列表数据
        const localChannels = getItem('user-channels')
        if (localChannels) {
          newChannels = localChannels
        } else {
          // 用户没有登录，也没有本地存储的频道，那就请求获取默认推荐的频道列表
          const res = await channels()
          newChannels = res.data.data.channels
        }
      }
      // 把处理好的数据放到 data 中以供模板使用
      this.channelLists = newChannels
    },
    // 更新频道
    onUpdateChannel (index) {
      this.active = index
    },
    // 关闭弹出层
    onCloseChannel () {
      this.onshowPopup = false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    this.getchannels()
  }
}
</script>
<style lang="less">
.home-container{
  .van-nav-bar__title{
    max-width: unset;
  }
  .search{
    width: 278px;
    height: 32px;
    background: #5babfb;
    .van-button__icon{
      width: 16px;
      height: 16px;
    }
    .van-button__text{
      font-size: 14px;
    }
  }
  .van-tabs{
    .wap-nav-placeholder{
      width: 33px;
      flex-shrink: 0;
    }
    .hamburger-tab{
      position: fixed;
      right: 0;
      width: 33px;
      height: 44px;
      background: #ffffff;
      display: flex;
      justify-content: center;
      align-items: center;
      opacity: .9;
      .van-icon{
        font-size: 24px;
      }
    }
  }
}
</style>
